package cn.lzwei.bilimall.product.service.impl;


import cn.lzwei.bilimall.product.dao.BrandDao;
import cn.lzwei.bilimall.product.entity.BrandEntity;
import cn.lzwei.bilimall.product.service.BrandService;
import cn.lzwei.bilimall.product.service.CategoryBrandRelationService;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Resource
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> queryWrapper=new QueryWrapper();
        //添加搜索条件
        if(!StringUtils.isNullOrEmpty(key)){
            queryWrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page=  this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 品牌修改：并更新在其他表中的冗余字段
     */
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        this.updateById(brand);
        //1.更新 品牌分类关联表
        Long brandId = brand.getBrandId();
        String name = brand.getName();
        categoryBrandRelationService.updateBrand(brandId,name);
        //TODO 品牌修改：更新在其他表中的冗余字段
    }

}